package cn.chenhao.pay.service.impl;

import cn.chenhao.basic.exception.BusinessException;
import cn.chenhao.basic.service.impl.BasicServiceImpl;
import cn.chenhao.order.service.IProductOrderService;
import cn.chenhao.pay.domain.AlipayInfo;
import cn.chenhao.pay.domain.PayBill;
import cn.chenhao.pay.mapper.AlipayInfoMapper;
import cn.chenhao.pay.mapper.PayBillMapper;
import cn.chenhao.pay.service.IPayBillService;
import cn.chenhao.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BasicServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        // 先判断当前支付单是否为空
        if(payBill == null){
            throw new BusinessException("大哥，支付单有问题，你再看一看呢");
        }
        // 再根据支付单id查询一波数据库是否为空
        PayBill bill = payBillMapper.loadById(payBill.getId());
        if(bill == null){
            throw new BusinessException("大哥，你的支付单真的有问题呀！！！");
        }

        // 区分支付类型  0 余额 1 支付宝 2 微信 3 银联
        switch (bill.getPayChannel().intValue()){
            case 1 :// 支付宝支付
                // 查询店铺保存的公钥私钥等数据
                // AlipayUtils工具类需要传入支付单数据和公钥
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info,bill);
            case 2 :// 微信
                //语句
                break; //可选
            case 3 ://银联
                //语句
                break; //可选
            default : //余额
                //语句
        }
        return null;
    }



    @Override
    public PayBill queryByUnionPaySn(String unionPaysn) {
        return payBillMapper.queryByUnionPaySn(unionPaysn);
    }
}
